/*####################################################################################
  CLASS : mydojo.dijix.AxTree
  ==================================
  - A simple tree widget with toolbar
  
  ####################################################################################*/
dojo.provide("mydojo.dijix.AxTree");

dojo.require("mydojo.core.AjaxHandler");
dojo.require("mydojo.dijix.SimpleTree");
dojo.require("dijit.layout.ContentPane");
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.form.Button");
dojo.require("dijit.form.TextBox");
dojo.require("dijit.Dialog");
dojo.require("dijit.Toolbar");

dojo.declare("mydojo.dijix.AxTree", [ dijit.layout.ContentPane , mydojo.core.AjaxHandler ] , {
		
		toolbarStruct	: [ 'r','a','e','d','f','ee','ep' ],
		// Region references...
		//struct 			: [], // depreciated.... seems like indexed array is not working when multiple DbGrid used in same TabView
		// widget refs....
		bcWt			: "",
		toolWt			: "",
		treeWt			: "",		
		// Events...	
		refreshUrl		: "",		// REFRESH tree with data from server
		addUrl			: "",		// ADD a node to the tree
		editUrl			: "",		// EDIT a tree node
		deleteUrl		: "",		// DELETE a node (and children) from tree
		refreshOnCreate : false,
		// ======================================================================
		//	Post create widget
		// ----------------------------------------------------------------------
		postCreate: function() {				
			this.inherited(arguments);	
			// create BC
			var rootNode = document.createElement("div");			
			this.bcWt = new dijit.layout.BorderContainer( {id:this.id+"_bc" , style:"width:100%;height:100%"} , rootNode );
			this.domNode.appendChild(this.bcWt.domNode);
			
			// create TOP CP						
			this.toolWt = new dijit.layout.ContentPane( {id:this.id+"_top" , style:"background:#A0C1D9;height:29px;overflow:hidden" , region:"top"});
			this.bcWt.addChild(this.toolWt);
			
			// create CENTER CP	-- this is actually SimpleGrid					
			this.treeWt = new mydojo.dijix.SimpleTree( {id:this.id+"_cen" , region:"center" });
			this.bcWt.addChild(this.treeWt);
					
			// create toolbar						
			this.createToolbar();					
								
			this.bcWt.startup();
			this.bcWt.layout();																	
			
			this.renderView = this.render;
			if( this.refreshOnCreate )
				this.rClicked();
		},
		// ======================================================================
		//	Render response from server 
		// ----------------------------------------------------------------------	
		render: function(response,event){
			console.log("Rendering DbTree...");
			this.treeWt.renderTree(response.model);
		},
		// ======================================================================
		//	Clean up this widget
		// ----------------------------------------------------------------------	
		clean: function(){
			console.log("Running DbTree.clean() function.")				
			this.toolWt.destroyDescendants();
			this.toolWt.destroy();
			this.treeWt.clean();
			this.treeWt.destroyDescendants();
			this.treeWt.destroy();
			this.bcWt.destroyDescendants();
			this.bcWt.destroy();
		},
		// ======================================================================
		//	Create toolbar
		// ----------------------------------------------------------------------
		createToolbar: function(){			
			var tb = new dijit.Toolbar({id:this.id+"_tb" , style:"height:32px"});						
			this.toolWt.domNode.appendChild(tb.domNode);
			for(var i = 0; i < this.toolbarStruct.length ; i++){
				switch(this.toolbarStruct[i]){
					case 'r':
						var btt = new dijit.form.Button({ id:this.id+"_refresh" , label:"Refresh" , iconClass:"navBarIcon iconRefresh" , showLabel:false });
						tb.addChild(btt);		
						dojo.connect(btt,"onClick",this,"rClicked");
						break;						
					case 'a':
						var btt = new dijit.form.Button({ id:this.id+"_add" , label:"Add" , iconClass:"navBarIcon iconAdd" , showLabel:false });
						tb.addChild(btt);		
						dojo.connect(btt,"onClick",this,"aClicked");
						break;						
					case 'e':
						var btt = new dijit.form.Button({ id:this.id+"_edit" , label:"Edit" , iconClass:"navBarIcon iconEdit" , showLabel:false });
						tb.addChild(btt);		
						dojo.connect(btt,"onClick",this,"eClicked");
						break;						
					case 'd':
						var btt = new dijit.form.Button({ id:this.id+"_delete" , label:"Delete" , iconClass:"navBarIcon iconDelete" , showLabel:false });
						tb.addChild(btt);		
						dojo.connect(btt,"onClick",this,"dClicked");
						break;									
				}
			}
			tb.startup();
		},
		// ======================================================================
		//	Toolbar Button Actions
		// ----------------------------------------------------------------------
		rClicked: function(){
			this.dispatch(this.refreshUrl);
		},
		// -------------------------------------
		aClicked: function(){
			this.showDlg("Add Menu" , null ,"go?eid="+this.addUrl);			
		},
		// -------------------------------------
		eClicked: function(){
			var node = this.treeWt.getFocusedNode();			
			if( !(node == null) )
				this.showDlg( "Edit Menu" , null , "go?eid="+this.editUrl+"&name="+node.name+"&type="+node.type );
			else
				console.error("Need to select a node in tree!!");
		},
		// -------------------------------------
		dClicked: function(){
		/*	var len = this.gridWt.grid.selection.getSelected().length;
			if(len >0){
				var id = [];			
				for(var i = 0 ; i < this.gridWt.grid.selection.getSelected().length ; i++){
					var rowId 	= this.gridWt.grid.selection.getSelected()[i];
					id[i] = this.getCellValue(rowId,0);					
				}
				if(dijit.byId("mainDialog") != null)
					dijit.byId("mainDialog").destroy();
				var dialog = new dijit.Dialog({id:"mainDialog" , title:"Confirm Delete"});			
				var ids = { 'id': id };
				var str = "<table border=0 cellpadding=0 cellspacing=5 width=\"380px\">"+
						  " <tr><td align=\"left\">" +
						  " <span class=normalText1 >Are you sure you want to delete?</span></td><td>" +			
						  " <tr><td align=\"left\" style=\"border-top: 1px solid rgb(134, 182, 204); padding-top: 5px;\">" +
						  "	<button dojoType=\"dijit.form.Button\">Yes</button>&nbsp;" + 
						  " <button dojoType=\"dijit.form.Button\"  onClick=\"dijit.byId('mainDialog').destroy();\">No</button>&nbsp;" +							
						  "</td></tr></table>";
				dialog.setContent( str );				
				var butt = dojo.query("button" , "mainDialog")[0].id;
				dojo.connect(dijit.byId(butt), "onClick" , this , "confirmDelete");			
				dialog.show();			
			}else
				console.error("Need to select a row in the grid!!");										
		*/
		},		
		// ======================================================================
		//	Render tree with given data
		// ----------------------------------------------------------------------
		renderGrid: function(data){
			if(data){
				this.treeWt.renderTree(data);
			}		
		},	
		// ======================================================================
		//	Clean the grid data
		// ----------------------------------------------------------------------
		clearTree: function(){
			
		},			
		// ======================================================================
		//	Enable Toolbar Buttons - based on records absent/present
		// ----------------------------------------------------------------------
		enableButtons: function(val){
			if(val>0){
				dijit.byId(this.id+"_edit").setAttribute('disabled',false);
				dijit.byId(this.id+"_delete").setAttribute('disabled',false);
			}
			else{
				dijit.byId(this.id+"_edit").setAttribute('disabled',true);				
				dijit.byId(this.id+"_delete").setAttribute('disabled',true);
			}
		},
		// ======================================================================
		//	Show Confirm Delete Dialog
		// ----------------------------------------------------------------------
		confirmDelete: function(){			
			dijit.byId('mainDialog').hide()
/*			var id = [];			
			for(var i = 0 ; i < this.gridWt.grid.selection.getSelected().length ; i++){
				var rowId 	= this.gridWt.grid.selection.getSelected()[i];
				id[i] = this.getCellValue(rowId,0);					
			}			
			var ids = { 'id': id };			
			this.dispatch(this.deleteUrl + "&" + dojo.objectToQuery(ids));
			*/
		}
});
